#ifndef AVALON_LIVETIMER_HPP
#define AVALON_LIVETIMER_HPP
#include <boost/asio.hpp>
#include <boost/system/system_error.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include "Avalon/Threading/Timer.hpp"

namespace Avalon {
namespace Threading {

  /*! \class LiveTimer
   *  \brief Implements a Timer using a live timer.
   */
  class LiveTimer : public Timer {
    public:

      //! Constructs a LiveTimer.
      /*!
        \param interval The time interval before expiring.
      */
      LiveTimer(boost::posix_time::time_duration interval);

      virtual ~LiveTimer();

      virtual void Start();

      virtual void Cancel(Out<bool> expired);

      virtual void SetExpiredSlot(const ExpiredSlot& slot);

    private:
      mutable boost::mutex m_mutex;
      boost::posix_time::time_duration m_interval;
      boost::asio::deadline_timer m_deadLineTimer;
      boost::function<void (const boost::system::error_code&)> m_deadlineSlot;
      boost::condition_variable m_cancelledCondition;
      boost::shared_ptr<ExpiredSlot> m_expiredSlot;

      void OnExpired(const boost::system::error_code& error);
  };
}
}

#endif // AVALON_LIVETIMER_HPP
